#include "bit.h"

////////////////////////////////////////////////////////////////////////////////
//Functionality - Sets bit on a PORTx
//Parameter: Takes in a uChar for a PORTx, the pin number and the binary value
//Returns: The new value of the PORTx
unsigned char SetBit(unsigned char pin, unsigned char number, unsigned char bin_value)
{
return (bin_value ? pin | (0x01 << number) : pin & ~(0x01 << number));
}

////////////////////////////////////////////////////////////////////////////////
//Functionality - Gets bit from a PINx
//Parameter: Takes in a uChar for a PINx and the pin number
//Returns: The value of the PINx
unsigned char GetBit(unsigned char port, unsigned char number)
{
return (( port & (0x01 << number) )>> number);
}



///////////////////////////////////////////////////////////////////////////
//These are unrelated to the above functions and I created the ones below
//the ling above.These are some functions I created after realizing that 
//for some setups it would be nice to set bits in groups. This will 
//make life easier to deal with later on.

//Gets a group of bits after setting a start location and a size of bits
//to get. Port is the value to get the bit group from. Size is the number of bits
//to get. Start tells the starting bit in the group. The rest of the bits are of
//a higher bit number after the start bit.
unsigned char GetBitGroup(unsigned char port, unsigned char size,
			 unsigned char start)
{
	//make the mask now
	unsigned char mask = ~(0xFF << size); //mask of size 3 == 0000 0111
	return (port & (mask << start))>>start;
}

//this sets a group of bits in the port. It is assumed the bits in value are aligned
//with 0 and continue size bits to be set.
unsigned char SetBitGroup(unsigned char port, unsigned char size, 
			  unsigned char start, unsigned char value)
{
	//make the mask now. Mask the values and set them now.
	unsigned char mask = ~(0xFF << size); //mask of size 3 == 0000 0111
	return (port & ~(mask << start)) | ((value & mask)<<start);

}

